package ui.commodity;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;
import blservice.document.DocumentBLService;
import vo.DocumentVo;
import vo.StockWarningVo;

import java.awt.Font;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.awt.Color;

public class StockWarningDocumentPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private List<StockWarningVo> vo;
	private JTable table;
	private JScrollPane jsp;

	public StockWarningDocumentPanel() {
		this.setLayout(null);
		this.setOpaque(false);
		vo=new ArrayList<StockWarningVo>();
		
		JLabel label = new JLabel("库存报警单");
		label.setForeground(Color.WHITE);
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label.setBounds(273, 10, 93, 21);
		add(label);

		table = new JTable() {
			private static final long serialVersionUID = 3028897666676861555L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.setBounds(47, 128, 535, 248);
		jsp = new JScrollPane(table);
		jsp.setBounds(47, 41, 535, 335);
		jsp.setVisible(true);
		this.add(jsp);
		getData();
		displayTable();
	}
	
	void getData(){
		DocumentBLService bl;
		try {
			bl = BLServiceManager.load库存报警单BLService(null);
			List<DocumentVo> temp = bl.getUnapproval();
			for(DocumentVo o:temp){
				vo.add((StockWarningVo)o);
			}
		} catch (RemoteException | MalformedURLException | SQLException
				| NotBoundException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
		
	}

	void displayTable() {
		DefaultTableModel model = new DefaultTableModel();
		model.setColumnIdentifiers(new String[] { "商品编号", "商品名称", "商品型号" ,"库存数量"});

		for (StockWarningVo o : vo) {
			model.addRow(new String[] {
					o.get商品编号(),o.get商品名称(),o.get商品型号(),
					Integer.toString(o.getNum())});
		}
		table.setModel(model);
	}
}